{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = r\"identity(3.4).sav\"\n",
    "df_raw = pandas.read_(r'identity(3.4).sav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw = df_raw.dropna(how=\"all\",axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw.shape\n",
    "df_raw.dropna(axis=0),shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw.dropna(axis=0,how=\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw.sort_values(by=[\"问卷编号\"],keep=\"first\",inplace=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw[\"会隐瞒身份吗\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyreadstat\n",
    "df_raw,metadata=pyreadstat.pyreadstat.read_sav(r'identity.sav',apply_value_formats=True,formats_as_ordered_category=True)\n",
    "df_raw['会隐瞒身份吗'].cat.categories\n",
    "'不会'in df['会隐瞒身份吗'].cat.categories\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyreadstatdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df,metadata = pyreadstat.pyreadstat.read_sav(r'identity(3.4).sav,apply_value_formets=true,formats_as_ordered_category=true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def wild_code_checking(value,df,col):\n",
    "    if value in df[col].cat.categories:\n",
    "        return '正常'\n",
    "    else:\n",
    "        return '异常'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wild_code_checking('会',df_raw,'会隐瞒身份吗')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw['会隐瞒身份吗'].apply(wild_code_checking,args=(df_raw,'会隐瞒身份吗'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw['异常数据']=df_raw['会隐瞒身份吗'].apply(wild_code_checking,args=(df_raw,'会隐瞒身份吗'))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "3f8fbc23b2a41515b896703ff7f99b01c6e80255122fee80a9cf9349d3bdee5d"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit (system)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
